/*
 * @Descripttion: 
 * @version: 
 * @Author: TT
 * @Date: 2023-10-25 09:52:37
 * @LastEditors: TT-hzy 
 * @LastEditTime: 2024-05-22 12:52:20
 */

import 'package:flutter/material.dart';
import 'package:hzy_project_template/assets/assets.dart';
import 'package:hzy_project_template/config/config_index.dart';
import 'package:hzy_project_template/models/image_normal_model.dart';
import 'package:hzy_project_template/utils/utils.dart';

import '../controllers/user_c.dart';

/// ======================= 全局共享Widget ======================= ///
///
///
///
/// ======================= 全局共享Widget ======================= ///

appSlogWidget({
  TextStyle? style,
  double? size = 24,
  Color? color = Colors.black,
  double? height,
}) {
  Widget body = Text(
    LaunchIdConfig.solg.tr,
    style: style ??
        HzyTextStyle.fontBold(
          size: size,
          color: color,
          height: height,
        ),
  );
  return body;
}

/*
 * 用户头像 
*/
userImgGlobalSharingWidget({
  double width = 100,
  double? radius,
  double? fontSize,
  int type = 1,
  bool isCanTap = true,
  String? heroStr,
  BoxFit? fit,
  Color? bgColor,
  TapCallback? tapCallback,
}) {
  Widget body = GetBuilder<UserC>(
    builder: (controller) {
      Widget bt = NormalUserInfoWidget.createUserInfoHeaderImgWidget(
        imageNormalModel: controller.imageNormalModel,
        width: width,
        radius: radius,
        fontSize: fontSize,
        type: type,
        isCanTap: isCanTap,
        heroStr: heroStr,
        tapCallback: tapCallback,
        fit: fit,
      );
      return bt;
    },
  );

  return body;
}

/*
 * 用户昵称 
*/
userNameGlobalSharingWidget({
  TextStyle? style,
  double? size = 16,
  Color? color = Colors.white,
  double? height,
  bool isCanTap = false,
  TapCallback? tapCallback,
  GlobalKey? globalKey,

  /// 0 显示系统默认 昵称 1 显示去登录
  int type = 0,
}) {
  Widget body = GetBuilder<UserC>(
    builder: (controller) {
      Widget bt = NormalUserInfoWidget.createUserNameWidget(
        name: controller.user?.nickName,
        type: type,
        size: size,
        color: color,
        height: height,
        style: style,
        globalKey: globalKey,
      );
      return bt;
    },
  );
  return body;
}

class NormalUserInfoWidget {
  /*
   * 配置用户用头像 
  */
  static createUserInfoHeaderImgWidget({
    ImageNormalModel? imageNormalModel,
    double width = 100,
    double? radius,
    double? fontSize,
    int type = 1,
    bool isCanTap = false,
    String? heroStr,
    BoxFit? fit,
    Color? bgColor,
    TapCallback? tapCallback,
  }) {
    double rd = radius == null ? width.r : radius.r;

    Widget body = imageNormalModel != null &&
            (imageNormalModel.assetPath != null ||
                imageNormalModel.byte != null ||
                imageNormalModel.url != null)
        ? conFigNetWorkImage(
            url: imageNormalModel.url,
            bytes: imageNormalModel.byte,
            assetPath: imageNormalModel.assetPath,
            width: width,
            height: width,
            borderRadius: BorderRadius.circular(
              rd,
            ),
            fit: fit ?? BoxFit.cover,
            heroStr: heroStr,
          )
        : createUserInfoPlaceholdWidget(
            width: width,
            radius: rd,
            size: fontSize,
            type: type,
            bgColor: bgColor,
            fit: fit ?? BoxFit.scaleDown,
          );

    body = isCanTap
        ? InkWell(
            onTap: tapCallback,
            child: body,
          )
        : body;
    return body;
  }

  /*
   * 配置默认头像
  */
  static createUserInfoPlaceholdWidget({
    double width = 100,
    double radius = 0,
    double? size = 18,
    int type = 1,
    Color? bgColor,
    BoxFit? fit = BoxFit.scaleDown,
  }) {
    if (width == 100) {
      size = 26;
    }
    Widget body = Container(
      width: width,
      height: width,
      decoration: BoxDecoration(
        color: HzyCommonColor().colthemes,
        borderRadius: BorderRadius.circular(radius),
      ),
      alignment: Alignment.center,
      padding: const EdgeInsets.all(2),
      child: Text(
        NoteLauncIdConfig.appName.tr,
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
        style: HzyTextStyle.fontBold(
          size: size,
          color: Colors.white,
        ),
      ),
    );

    body = type == 1
        ? body
        : Container(
            width: width,
            height: width,
            decoration: BoxDecoration(
              color: bgColor ?? HzyCommonColor().backgroudgrey,
              borderRadius: BorderRadius.circular(
                radius,
              ),
            ),
            alignment: Alignment.center,
            child: Image.asset(
              Assets.commDefaultUser,
              width: width / 2,
              height: width / 2,
              fit: fit,
            ),
          );

    return body;
  }

/*
   * 配置用户昵称
  */
  static createUserNameWidget({
    String? name,
    TextStyle? style,
    double? size = 16,
    Color? color = Colors.white,
    double? height,
    GlobalKey? globalKey,

    /// 0 显示系统默认 昵称 1 显示去登录
    int type = 0,
  }) {
    Widget body = Text(
      name ??
          (type == 0
              ? NoteLauncIdConfig.nickName.tr
              : "${LaunchIdConfig.click.tr}${LaunchIdConfig.login.tr}/${LaunchIdConfig.register.tr}"),
      key: globalKey,
      style: style ??
          HzyTextStyle.fontMedium(
            size: size,
            color: color,
            height: height,
          ),
    );
    return body;
  }
}
